package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.service.EmployeeService;

public class EditPwdServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		Employee emp = (Employee) session.getAttribute("employee");
		
		String name = emp.getName();
		if("POST".equals(request.getMethod())){
			response.setContentType("text/html");
			
			PrintWriter out = response.getWriter();
			String old = request.getParameter("old");
			String newpwd = request.getParameter("newpwd");
			EmployeeService es = new EmployeeService();
			
			Employee em = es.checkLogin(name, old);
			
			
			if(em != null){
				em.setPassword(newpwd);
				es.update(em);
				out.print("1");
				
			}else{
				out.print("0");
			}
			
		}
	}

}
